/**
 * 
 */
package com.nettest.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author Andy Wu
 *
 */
public class DateUtil {
	public static final DateFormat df1 = new SimpleDateFormat("yyyy-MM");
	public static final DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
	public static final DateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
	public static final DateFormat df4 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	public static int getWeekdayOfDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek =calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek;
	}
	public static Date addDay(Date date, int n){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, n);
		return calendar.getTime();
	}
	
	public static int getDaysOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	public static int getDaysOfMonth(String date) throws ParseException{
		Date theDate = string2Date(date);
		return getDaysOfMonth(theDate);
	}
	public static Date getFirstDayOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	/** 获取月末一天，时间是00:00:00  */
	public static Date getLastDayOfMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 
	 * @author Andy Wu
	 * 2012-9-25 上午2:03:20
	 * @param date String, format: 'yyyy-MM'、yyyy-MM-dd 或者yyyy-MM-dd HH:mm:SS
	 * @return
	 * @throws ParseException 
	 */
	public static Date getLastDayOfMonth(String date) throws ParseException{
		
		Date theDate = string2Date(date);
		return getLastDayOfMonth(theDate);
	}
	public static Date string2Date(String date) {
		if (date == null) {
			return null;
		}
		
		Date theDate = null;
		try{
			theDate = df3.parse(date);
		}catch(ParseException pe1){
			try{
				theDate = df4.parse(date);
			}catch(ParseException pe2){
				try{
					theDate = df2.parse(date);
				} catch(ParseException pe3){
					try {
						theDate = df1.parse(date);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return theDate;
	}
	/**
	 * 
	 * @author Andy Wu
	 * 2012-9-25 上午2:03:20
	 * @param date String, format: 'yyyy-MM'、yyyy-MM-dd 或者yyyy-MM-dd HH:mm:SS
	 * @return
	 * @throws ParseException 
	 */
	public static Date getFirstDayOfMonth(String date) throws ParseException{
		
		Date theDate  = string2Date(date);
		return getFirstDayOfMonth(theDate);
	}
	
	public static String date2Str(Date date, String format) {
		SimpleDateFormat formater = new SimpleDateFormat(format);
		return formater.format(date);
	}
	
	public static int getDayIndexOfMonth(Date date) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		int dayOfMonthIndex = cale.get(Calendar.DAY_OF_MONTH);
		return dayOfMonthIndex;
	}
	public static int getDayIndexOfMonth(String date) throws ParseException {
		Calendar cale = Calendar.getInstance();
		cale.setTime(string2Date(date));
		int dayOfMonthIndex = cale.get(Calendar.DAY_OF_MONTH);
		return dayOfMonthIndex;
	}

	
	public static boolean isSaturday(String dateStr) throws ParseException{
		int n = getWeekdayOfDate(string2Date(dateStr));
		return (n == 7);
	}
	public static boolean isSunday(String dateStr) throws ParseException{
		int n = getWeekdayOfDate(string2Date(dateStr));
		return (n == 1);
	}
	public static  boolean isLastDayOfMonth(String date) throws ParseException{
		Date d1 = DateUtil.getLastDayOfMonth(date);
		Date d2 = DateUtil.string2Date(date);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(d1);
		int m1 = c1.get(Calendar.DAY_OF_MONTH);
		c1.setTime(d2);
		int m2 = c1.get(Calendar.DAY_OF_MONTH);
		return m1 == m2;
	}
	/**
	 * @author Andy Wu
	 * 2012-10-26 上午4:39:59
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		return c1.get(Calendar.YEAR);
	}
	/**
	 * @author Andy Wu
	 * 2012-10-26 上午8:54:11
	 * @param parseInt
	 * @return
	 */
	public static Date getStartDayOfYear(int year) {
		Calendar c1 = Calendar.getInstance();
		c1.set(year, 0, 1, 0, 0, 0);
		return c1.getTime();
	}
	/**
	 * @author Andy Wu
	 * 2012-10-26 上午8:56:31
	 * @param date
	 * @param i
	 * @return
	 */
	public static Date addYear(Date date, int i) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		c1.add(Calendar.YEAR, i);
		return c1.getTime();
	}
	public static Date getEndDayOfYear(int year) {
		Calendar c1 = Calendar.getInstance();
		c1.set(year, 11, 31, 0, 0, 0);
		return c1.getTime();
	}
	public static void main(String[] args) {
		System.out.println(ConvertUtil.date2Str(getStartDayOfYear(2012), "yyyy-MM-dd") );
	}
}
